package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.goods.TbGoods;
import com.dk.pojo.goods.TbGoodsDesc;
import com.dk.pojo.item.TbItem;
import com.dk.pojo.seller.TbSeller;
import com.dk.service.goods.GoodsService;
import com.dk.service.item.ItemCatService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("goods")
public class GoodsController {

    private final Logger logger = LoggerFactory.getLogger(GoodsController.class);

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private GoodsService goodsService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("listGoods")
    @ResponseBody
    public Result listGoods() {
        try {
            List<TbGoods> list = goodsService.listGoods();
            return Result.response(ResultEnum.OK).setData(list);
        } catch (Exception e) {
            logger.error("商品查询列表错误:" ,e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    /**
     * 商品审核
     */
    @RequestMapping("goodsEach")
    @ResponseBody
    public Result goodsEach(String status,@RequestParam("ids[]") Long[] ids) {
        try {
            int i = goodsService.goodsEach(status,ids);
            for (int j = 0; j < ids.length; j++) {
                rocketMQTemplate.convertAndSend("dongdongshop",ids[i]);
                rocketMQTemplate.convertAndSend("es",ids[i]);
            }
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("商品审核错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

}
